package com.sys.basics.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.sys.basics.config.annotation.SysLogAnnotation;
import com.sys.basics.config.jsonRes.JsonResult;
import com.sys.basics.config.jsonRes.PageParam;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;

/**
 * @author: ares
 * @date: 2021-09-30
 * @description: 通用接口
 */
@Validated
public class AbstractController<S extends IService<T>, T> {

    @Autowired
    protected S baseService;

    protected JsonResult<T> result = new JsonResult<T>();

    /**
     * @author: ares
     * @date: 2021-09-30 14:54:50
     * @description: 查询对象
     * @param id
     * @return JsonResult<T>
     */
    @SysLogAnnotation("【继承】根据表ID查询详情")
    @ApiOperation("【继承】根据表ID查询详情")
    @ApiImplicitParam(name = "id", value = "表ID", paramType = "query")
    @GetMapping("/getById")
    public JsonResult<T> getById(@NotNull(message = "id不能为空") Long id) {
        T obj = baseService.getById(id);
        if (null != obj) {
            result.success(obj);
        } else {
            result.error("查询对象不存在！");
        }
        return result;
    }

    /**
     * @author: ares
     * @date: 2021-09-30 14:54:50
     * @description: 删除对象
     * @param id
     * @return JsonResult<T>
     */
    @SysLogAnnotation("【继承】根据表ID删除数据")
    @ApiOperation("【继承】根据表ID删除数据")
    @ApiImplicitParam(name = "id", required = true, value = "表ID", paramType = "query")
    @DeleteMapping("/removeById")
    public JsonResult removeById(@NotNull(message = "id不能为空") Long id) {
        T obj = baseService.getById(id);
        if (null != obj) {
            boolean rsg = baseService.removeById(id);
            if (rsg) {
                result.success("删除成功");
            } else {
                result.error("删除失败！");
            }
        } else {
            result.error("删除的对象不存在！");
        }
        return result;
    }

    /**
     * @author: ares
     * @date: 2021-09-30 14:54:50
     * @description: 添加对象
     * @param entity
     * @return JsonResult<T>
     */
    @SysLogAnnotation("【继承】新增数据")
    @ApiOperation("【继承】新增数据")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody @Valid T entity) {
        if (null != entity) {
            boolean rsg = baseService.save(entity);
            if (rsg) {
                result.success("添加成功");
            } else {
                result.error("添加失败！");
            }
        } else {
            result.error("请传入正确参数！");
        }
        return result;
    }

    /**
     * @author: ares
     * @date: 2021-09-30 14:54:50
     * @description: 修改对象
     * @param entity
     * @return JsonResult<T>
     */
    @SysLogAnnotation("【继承】更新数据")
    @ApiOperation("【继承】更新数据")
    @PutMapping("/update")
    public JsonResult update(@RequestBody @Valid T entity) {
        if (null != entity) {
            boolean rsg = baseService.updateById(entity);
            if (rsg) {
                result.success("修改成功");
            } else {
                result.error("修改失败！");
            }
        } else {
            result.error("请传入正确参数！");
        }
        return result;
    }

    /**
     * @author: ares
     * @date: 2021-09-30 14:54:50
     * @description: 分页条件查询
     * @param param
     * @return JsonResult<IPage<T>>
     */
    @SysLogAnnotation("【继承】分页查询")
    @ApiOperation("【继承】分页查询")
    @PostMapping("/getPages")
    public JsonResult<IPage<T>> getPages(@RequestBody PageParam<T> param) {
        JsonResult<IPage<T>> returnPage = new JsonResult<IPage<T>>();
        Page<T> page = new Page<T>(param.getPageNum(), param.getPageSize());
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
        queryWrapper.setEntity(param.getParam());
        //分页数据
        IPage<T> pageData = baseService.page(page, queryWrapper);
        returnPage.success(pageData);
        return returnPage;
    }

    /**
     * @author: ares
     * @date: 2021-09-30 14:54:50
     * @description: 查询所有
     * @return JsonResult<List<T>>
     */
    @SysLogAnnotation("【继承】查询表中所有记录")
    @ApiOperation("【继承】查询表中所有记录")
    @GetMapping("/getAll")
    public JsonResult<List<T>> getAll() {
        JsonResult<List<T>> result = new JsonResult<List<T>>();
        List<T> list = baseService.list();
        result.success(list);
        return result;
    }
}
